<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="工作ID" prop="workId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workId"-->
      <!--          placeholder="请输入工作ID"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="流程ID" prop="flowId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.flowId"-->
      <!--          placeholder="请输入流程ID"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="流程ID" prop="billName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.billName"-->
      <!--          placeholder="请输入流程ID"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作表单id" prop="workSheetId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workSheetId"-->
      <!--          placeholder="请输入工作表单id"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="票种名称" prop="billName">
        <el-input
          v-model="queryParams.billName"
          placeholder="请输入票种名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单编号" prop="workSheetCode">
        <el-input
          v-model="queryParams.workSheetCode"
          placeholder="请输入表单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作负责人名称" prop="workHeadName">
        <el-input
          v-model="queryParams.workHeadName"
          placeholder="请输入工作负责人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="终结流程状态" prop="workSheetEndState">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workSheetEndState"-->
      <!--          placeholder="请输入终结流程状态"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作终结日期" prop="workEndTime">-->
      <!--        <el-date-picker clearable size="small"-->
      <!--          v-model="queryParams.workEndTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="选择工作终结日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作票终结时间" prop="endTime">-->
      <!--        <el-date-picker clearable size="small"-->
      <!--          v-model="queryParams.endTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="选择工作票终结时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="计划开始时间" prop="planBeginTime">-->
      <!--        <el-date-picker clearable size="small"-->
      <!--          v-model="queryParams.planBeginTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="选择计划开始时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="计划结束时间" prop="planEndTime">-->
      <!--        <el-date-picker clearable size="small"-->
      <!--          v-model="queryParams.planEndTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="选择计划结束时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作负责人名称" prop="workHeadName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workHeadName"-->
      <!--          placeholder="请输入工作负责人名称"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作名称" prop="workName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workName"-->
      <!--          placeholder="请输入工作名称"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="创建时间" prop="createDatetime">-->
      <!--        <el-date-picker clearable size="small"-->
      <!--          v-model="queryParams.createDatetime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="选择创建时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="延期流程状态" prop="workSheetDelayState">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workSheetDelayState"-->
      <!--          placeholder="请输入延期流程状态"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['flowable:EndedBill:add']"-->
      <!--        >新增</el-button>-->
      <!--        <el-button-->
      <!--          size="mini"-->
      <!--          type="text"-->
      <!--          icon="el-icon-tickets"-->
      <!--          @click="handleFlowRecord(scope.row)"-->
      <!--        >详情</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['flowable:EndedBill:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['flowable:EndedBill:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flowable:EndedBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EndedBillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="工作ID" align="center" prop="workId" />-->
      <!--      <el-table-column label="流程ID" align="center" prop="flowId" />-->
      <!--      <el-table-column label="流程ID" align="center" prop="billName" />-->
      <!--      <el-table-column label="表单编号" align="center" prop="workSheetCode" />-->
      <!--      <el-table-column label="工作表单id" align="center" prop="workSheetId" />-->
      <el-table-column label="表单编号" align="center" prop="workSheetCode" />
      <el-table-column label="票种名称" align="center" prop="billName" />
      <!--      <el-table-column label="终结流程状态" align="center" prop="workSheetEndState" />-->
      <!--      <el-table-column label="工作终结日期" align="center" prop="workEndTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.workEndTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="工作票终结时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作负责人名称" align="center" prop="workHeadName" />
      <!--      <el-table-column label="工作名称" align="center" prop="workName" />-->
      <!--      <el-table-column label="创建时间" align="center" prop="createDatetime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createDatetime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="延期流程状态" align="center" prop="workSheetDelayState" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleFlowRecord(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewFlow(scope.row)"
            v-hasPermi="['process:EndedBill:edit']"
          >审批过程</el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['flowable:EndedBill:edit']"-->
          <!--          >修改</el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['flowable:EndedBill:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改已完成的工作票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工作ID" prop="workId">
          <el-input v-model="form.workId" placeholder="请输入工作ID" />
        </el-form-item>
        <el-form-item label="流程ID" prop="flowId">
          <el-input v-model="form.flowId" placeholder="请输入流程ID" />
        </el-form-item>
        <el-form-item label="流程ID" prop="billName">
          <el-input v-model="form.billName" placeholder="请输入流程ID" />
        </el-form-item>
        <el-form-item label="工作表单id" prop="workSheetId">
          <el-input v-model="form.workSheetId" placeholder="请输入工作表单id" />
        </el-form-item>
        <el-form-item label="表单编号" prop="workSheetCode">
          <el-input v-model="form.workSheetCode" placeholder="请输入表单编号" />
        </el-form-item>
        <el-form-item label="终结流程状态" prop="workSheetEndState">
          <el-input v-model="form.workSheetEndState" placeholder="请输入终结流程状态" />
        </el-form-item>
        <el-form-item label="工作终结日期" prop="workEndTime">
          <el-date-picker clearable size="small"
                          v-model="form.workEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择工作终结日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作票终结时间" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择工作票终结时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planBeginTime">
          <el-date-picker clearable size="small"
                          v-model="form.planBeginTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndTime">
          <el-date-picker clearable size="small"
                          v-model="form.planEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择计划结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作负责人名称" prop="workHeadName">
          <el-input v-model="form.workHeadName" placeholder="请输入工作负责人名称" />
        </el-form-item>
        <el-form-item label="工作名称" prop="workName">
          <el-input v-model="form.workName" placeholder="请输入工作名称" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDatetime">
          <el-date-picker clearable size="small"
                          v-model="form.createDatetime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="延期流程状态" prop="workSheetDelayState">
          <el-input v-model="form.workSheetDelayState" placeholder="请输入延期流程状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEndedBill, getEndedBill, delEndedBill, addEndedBill, updateEndedBill, exportEndedBill } from "@/api/flowable/EndedBill";
import {EndedBillTrace} from "@/api/process/EndedBill";

export default {
  name: "EndedBill",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 已完成的工作票表格数据
      EndedBillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workId: null,
        flowId: null,
        billName: null,
        workSheetId: null,
        workSheetCode: null,
        workSheetEndState: null,
        workEndTime: null,
        endTime: null,
        planBeginTime: null,
        planEndTime: null,
        workHeadName: null,
        workName: null,
        createDatetime: null,
        workSheetDelayState: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workId: [
          { required: true, message: "工作ID不能为空", trigger: "blur" }
        ],
        flowId: [
          { required: true, message: "流程ID不能为空", trigger: "blur" }
        ],
        workSheetId: [
          { required: true, message: "工作表单id不能为空", trigger: "blur" }
        ],
        createDatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {



    /** 查询已完成的工作票列表 */
    getList() {
      this.loading = true;
      listEndedBill(this.queryParams).then(response => {
        this.EndedBillList = response.rows;
        console.log(this.EndedBillList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workId: null,
        flowId: null,
        billName: null,
        workSheetId: null,
        workSheetCode: null,
        workSheetEndState: null,
        workEndTime: null,
        endTime: null,
        planBeginTime: null,
        planEndTime: null,
        workHeadName: null,
        workName: null,
        createDatetime: null,
        workSheetDelayState: null
      };
      this.resetForm("form");
    },
    /** 流程流转记录 */
    handleFlowRecord(row){
      console.log(row.workSheetCode)
      this.$router.push({ path: '/flowable/endbilltrace/index',
        query: {
          workSheetCode: row.workSheetCode,
        }})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加已完成的工作票";
    },

    /** 审批记录按钮操作 */
    viewFlow(row) {
      let that=this;
      this.reset();
      const workId = row.workId;
      this.open = true;
      this.title = row.billName+row.workSheetCode+"工作流程图";
      console.log(row);
      that.timer = setInterval(() => {//等待el-dialog加载完毕，再调用flow()函数
        if (document.readyState === 'complete') {
          console.log("ready to load el-dialog and flow-chart");
          this.FlowDetail(row);
          window.clearInterval(that.timer);
        }
      }, 100);
    },
    FlowDetail(row){
      console.log(this.ids[0]);
      let that = this;
      console.log(row)
      var data={workId:row.workId,
        workSheetId:row.workSheetId,
        flowId:row.flowId,
        workSheetCode:row.workSheetCode};
      EndedBillTrace(data).then(response => {//根据工作单编码、工作id、流程id、工作单编码查找工作票轨迹
        // this.$modal.msgSuccess("插入成功");
        this.BillTraceList=response.data;//接收该工作票包括的所有流程
        console.log(this.BillTraceList);
        console.log("执行构造流程图函数");
        this.flow();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workId = row.workId || this.ids
      getEndedBill(workId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改已完成的工作票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workId != null) {
            updateEndedBill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEndedBill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workIds = row.workId || this.ids;
      this.$confirm('是否确认删除已完成的工作票编号为"' + workIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delEndedBill(workIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有已完成的工作票数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportEndedBill(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>



